<?php
class MemberAction extends BaseAction {
	
	public function index(){
		
		$tab = $_GET['tab'];
		
		if($tab == null || $tab == ''){
			$tab = 'account';
		}
		
		$loginUser = D('User')->find($_SESSION[C('USER_AUTH_KEY')]);
//		dump($loginUser);
		$this->assign('loginUser', $loginUser);
		$this->assign('tab', $tab);
		$this->display();
	}
	
	public function changepassword(){
		
		$newpassword=trim($_POST['newpassword']);
		$repassword=trim($_POST['newpassword2']);
		$password=trim($_POST['oldpassword']);
		
    	$loginUser = M('User')->find($_SESSION[C('USER_AUTH_KEY')]);
    	
    	if(md5($password)!= $loginUser['password']){
    		$this->error(L("输入密码有误！"));
    	}
    	
    	if($newpassword != $repassword){
    		$this->error(L("两次输入的密码不一致！"));
    	}
    	$Member = M('User');
    	$data['id']= intval($loginUser['id']);
    	$data['password']= md5($newpassword);
    	
        if($Member->save($data)){
			$this->success(L('_INSERT_SUCCESS_'));
        }else{
			$this->error($Member->getError());
        }
		
	}
	
	public function changesetting(){
		
    	$loginUser = M('User');
    	
        if($loginUser->create()){
        	
        	if($loginUser->save()){
				$this->success(L('_INSERT_SUCCESS_'));
        	}else{
        		$this->error($loginUser->getError());
        	}
        }else{
			$this->error($loginUser->getError());
        }
		
	}
	
	public function changeprofile(){
		
		$newpassword=trim($_POST['newpassword']);
		$repassword=trim($_POST['newpassword2']);
		$password=trim($_POST['oldpassword']);
		
		$email=trim($_POST['email']);
		
    	$loginUser = M('User')->find($_SESSION[C('USER_AUTH_KEY')]);
    	
    	if(md5($password)!= $loginUser['password']){
    		$this->error(L("输入密码有误！"));
    	}
    	
    	if($newpassword != $repassword){
    		$this->error(L("两次输入的密码不一致！"));
    	}
    	$Member = M('User');
    	$data['id']= intval($loginUser['id']);
    	$data['password']= md5($newpassword);
    	if($email != ''){
    		$data['email'] = $email;
    	}
    	
        if($Member->save($data)){
//			$this->assign('jumpUrl',__APP__.'/Member/account');
			$this->success(L('_INSERT_SUCCESS_'));
        }else{
			$this->error($Member->getError());
        }
		
//		$this->display();
	}
}
?>